package com.sevennotes.classtable.ui.pages.components

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowRight
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.sevennotes.classtable.R


@Composable
fun NumChanger(
    pointValue: Int,
    onPointChanged: (Int) -> Unit,
    unitText: String = "分"
) {
    val tempPoint = pointValue.toString()
    Row(
        verticalAlignment = Alignment.CenterVertically
    ) {
        IconButton(onClick = { onPointChanged(pointValue - 1) }) {
            Icon(
                painter = painterResource(id = R.drawable.ic_outline_remove_24),
                contentDescription = "minus"
            )
        }
        Box(modifier = Modifier.width(80.dp)){
            OutlinedTextField(
                modifier = Modifier.height(50.dp),
                value = tempPoint,
                onValueChange = { s->
                    val text = s.filter { it.isDigit() || it == '-' }
                    try {
                        onPointChanged(text.toInt())
                    }catch (e: Exception) {
                        onPointChanged(0)
                    }
                },
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                singleLine = true,
                textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.End)
            )
        }
        Text(text = unitText)
        IconButton(onClick = { onPointChanged(pointValue + 1) }) {
            Icon(
                painter = painterResource(id = R.drawable.ic_outline_add_24),
                contentDescription = "add"
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun NumChangerPreview() {
    NumChanger(pointValue = 0, onPointChanged = {})
}